package offer;

/**
 * @Author Elephas
 * @Date 2022/2/10
 **/
@FunctionalInterface
public interface ReorderSpaces {
    /**
     * You are given a string text of words that are placed among some number of spaces.
     * Each word consists of one or more lowercase English letters and are separated by at least one space.
     * It's guaranteed that text contains at least one word.
     *
     * Rearrange the spaces so that there is an equal number of spaces between every pair of adjacent words and that number is maximized.
     * If you cannot redistribute all the spaces equally,
     * place the extra spaces at the end, meaning the returned string should be the same length as text.
     *
     * Return the string after rearranging the spaces.
     * @param text
     * @return
     */
    String reorderSpaces(String text);
}
class ReorderSpacesImpl2 implements ReorderSpaces{
    static final String TEST_01 = "  this   is  a sentence ";

    public static void main(String[] args) {
        new ReorderSpacesImpl2().reorderSpaces(TEST_01);
    }


    private int spacesNum;
    private int wordsNum;
    @Override
    public String reorderSpaces(String text) {
        if("".equals(text) || null == text){
            return "";
        }
        countSpacesAndWords(text);
        String ans;
        if(spacesNum == 0){
            ans = text;
        }else if(wordsNum == 1){
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < text.length(); i++) {
                char curChar = text.charAt(i);
                if(curChar != ' '){
                    stringBuilder.append(curChar);
                }
            }
            for (int i = 0; i < spacesNum; i++) {
                stringBuilder.append(' ');
            }
            ans = stringBuilder.toString();
        }else {
            ans = reorderSpaceHelper(text);
        }
        return ans;
    }
    private String reorderSpaceHelper(String text){
        int intervalLength = spacesNum / (wordsNum - 1);
        int reminderSpaces = spacesNum % (wordsNum - 1);
        StringBuilder ans = new StringBuilder();
        int n = text.length();
        int curWords = 0, pos = 0;

        // 去除开头的空白
        while(pos < n){
            char curChar = text.charAt(pos);
            if(curChar == ' '){
                pos++;
            }else{
                break;
            }
        }
        // 开始重排返回结果
        while(pos < n){
            char curChar = text.charAt(pos);
            pos++;
            if(curChar != ' '){
                ans.append(curChar);
            }else{
                curWords++;
                // 过滤所有空白
                while(pos < n){
                    curChar = text.charAt(pos);
                    if(curChar == ' '){
                        pos++;
                    }else{
                        break;
                    }
                }
                // 补上重排的空格
                if(curWords < wordsNum) {
                    for (int i = 0; i < intervalLength; i++) {
                        ans.append(' ');
                    }
                }
            }
        }
        for (int i = 0; i < reminderSpaces; i++) {
            ans.append(' ');
        }
        return ans.toString();
    }


    private void countSpacesAndWords(String text){
        spacesNum = 0;
        wordsNum = 0;
        if(text.charAt(0) != ' '){
            wordsNum++;
        }else{
            spacesNum++;
        }
        int preChar = text.charAt(0);
        int n = text.length();
        for (int i = 1; i < n; i++) {
            int curChar = text.charAt(i);
            if(preChar == ' ' && curChar != ' '){
                wordsNum++;
            }
            if(curChar == ' '){
                spacesNum++;
            }
            preChar = curChar;
        }
    }
}